import api from '../api/index'
export default {
  changeAcNum({state, commit, getters}) {
    // 异步写在action,等发出去的请求拿到数据了,然后commit一个mutation,commit时候带上数据
    let data = {
      shopId: '00000000040',
      appid: '82dfd052838bec7c',
      appsecret: '2021d0268b7da71410d23cff1f3cb38b'
    }
    api.getApiToken(data).then(res => {
      console.log(res, 'actoken');
      if (res.ResultCode === 200) {
        commit('getToken', res.ReturnData.Token)
      }
    })
  },
  getMyToken({commit}) {
    return new Promise((resolve, reject) => {
      let data = {
        shopId: '00000000040',
        appid: '82dfd052838bec7c',
        appsecret: '2021d0268b7da71410d23cff1f3cb38b'
      }
      api.getApiToken(data)
        .then(res => {
          console.log('请求token');
          localStorage.setItem('actoken', res.ReturnData.Token)
          commit('getToken', res.ReturnData.Token)
          resolve()
        })
        .catch(err => {
          reject()
        })
    })
  },
  getGoods({dispatch, state}) {
    dispatch('getMyToken').then(res => {
      let data = {
        Token: state.actoken,
        user_id: '',
      }
      api.getGoodsList(data).then(res => {
        console.log(res, 'good');
        if (res.ResultCode === 200) {
          state.goodsList = res.ReturnData.list
          state.goodsList.forEach(item => {
            state.imagesList.push(item.goods_thumb)
          })
          // state.goodsList = state.goodsList.splice(0,5)
        }
      })
    })
  }
}